package com.example.chat.view

import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.example.chat.viewmodel.TopicListState
import com.example.chat.model.Topic
import com.example.featuresdk.theme.DemoTheme
import kotlinx.coroutines.flow.flowOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.sql.Timestamp

@RunWith(AndroidJUnit4::class)
@MediumTest
class TopicListScreenTest {

    val mockTopics: List<Topic> = listOf(
        Topic(1000,"Topic 1", count=5, message = "Hello", timestamp = Timestamp(System.currentTimeMillis())),
        Topic(1001,"Topic 2", count=100, message = "Good", timestamp = Timestamp(System.currentTimeMillis())),
        Topic(1002,"Topic 3", count=0, message = "No count", timestamp = Timestamp(System.currentTimeMillis()))
    )

    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun TopicListTest() {

        // Start the app
        composeTestRule.setContent {
            DemoTheme {
                TopicListScreen(TopicListState().apply {
                   this.topics = flowOf(mockTopics)
                }, LayoutConstraints.init(LocalConfiguration.current))
            }
        }

        composeTestRule.onNode(hasText("Topic 1")).assertExists("Topic 1  should exist")
        composeTestRule.onNodeWithText("Topic 3").assertExists("Topic 3  should exist")

        composeTestRule.onAllNodesWithTag("topic").assertCountEquals(3)

        composeTestRule.onAllNodesWithTag("topic").onFirst().performClick()
    }
}